﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using WordTemplate1.Objets;
using System.Threading;
using Microsoft.Office.Interop.Word;
using WordTemplate1.Formulaire;
using Methodes.WordTemplate1;

namespace WordTemplate1
{
    /// <summary>
    /// Panel d'information sur les modifications serveur ou document
    /// </summary>
    public partial class UCInformation : UserControl
    {
        /// <summary>
        /// Permet de mettre à jour l'affichage des données modiffiés
        /// </summary>
        public void ActualiseInformation()
        {
            lstInformation.DataSource = Globals.ThisDocument.taskPanel.ListElement.Where(element => element.IsModify == true).ToList();
            lstInformation.DisplayMember = "Title";
        }

        public UCInformation()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Permet de mettre à jour qu'une propriété du workitem a la fois
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MAJWorkitem_Click(object sender, EventArgs e)
        {
            if (lstInformation.SelectedItem != null)
            {
                if (lstInformation.SelectedItem is Element)
                {
                    var element = lstInformation.SelectedItem as Element;

                    // Modifié un élément de type champs de workitem
                    if (element.Type == TypeElement.Field)
                    {
                        var el = element as EField;
                        try
                        {
                            el.Save();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            if (el.Workitem.IsOpen)
                            {
                                el.Workitem.Close();
                            }

                            el.IsModify = false;
                            this.ActualiseInformation();
                        }
                    }
                }
            }
        }

        private void lstInformation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstInformation.Items.Count > 0)
            {
                if (lstInformation.SelectedItem != null)
                {
                    if (lstInformation.SelectedItem is Element)
                    {
                        Element w = lstInformation.SelectedItem as Element;

                        menuDetail.Text = "See the detail";
                        menuMAJWorkitem.Text = "Update the WorkItem";
                        menuMAJDocument.Text = "Update the document";

                    }
                    else
                    {
                        menuDetail.Text = "Please select an item...";
                        menuMAJWorkitem.Text = "Please select an item...";
                        menuMAJDocument.Text = "Please select an item...";
                    }
                }
            }
            else
            {
                this.ActualiseInformation();
            }
        }

        private void DetailChange_Click(object sender, EventArgs e)
        {
            if (lstInformation.SelectedItem != null)
            {
                if (lstInformation.SelectedItem is EField)
                {
                    EField w = lstInformation.SelectedItem as EField;

                    DetailChange dc = new DetailChange();
                    dc.Changement = w;
                    dc.ShowDialog();
                }
            }

        }

        private void mettreÀJourLeDocumentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstInformation.Items.Count > 0)
            {
                if (lstInformation.SelectedItem is EField)
                {
                    var element = lstInformation.SelectedItem as EField;

                    foreach (string cc in element.ContentcontrolsId)
                    {
                        try
                        {
                            var c = (from search in Globals.ThisDocument.ContentControls.Cast<ContentControl>()
                                     where search.ID == cc
                                     select search).FirstOrDefault();

                            if (element.ValueTFS != c.Range.Text)
                            {
                                c.Range.Text = element.ValueTFS;
                                element.IsModify = false;
                                this.ActualiseInformation();
                            }
                            else
                            {
                                element.IsModify = false;
                                this.ActualiseInformation();
                            }
                        }
                        catch (Exception ex)
                        {
                            element.IsModify = false;
                            this.ActualiseInformation();
                        }
                    }
                }
            }
        }

        private void actualiserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ActualiseInformation();
        }

        private void annulerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstInformation.Items.Count > 0)
            {
                Element element = lstInformation.SelectedItem as Element;

                element.IsModify = false;
                this.ActualiseInformation();
            }
        }
    }
}
